//
//  ForgetPwdViewController.m
//  zhijian
//
//  Created by lcb on 2019/4/5.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "ForgetPwdViewController.h"
#import "LTLimitTextField.h"
@interface ForgetPwdViewController ()
@property (nonatomic, strong) UIView * loginView;
@property (nonatomic, strong) LTLimitTextField *companyField;
@property (nonatomic, strong) LTLimitTextField *accountField;
@property (nonatomic, strong) LTLimitTextField *pwdField;
@property (nonatomic, strong) LTLimitTextField *repwdField;
@property (nonatomic, strong) UIButton * sendButton;
@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // Do any additional setup after loading the view.
    [self hyb_navWithLeftImage:@"back" title:@"找回密码" leftClicked:^(UIButton *sender) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    [self initUI];
}
-(void)initUI
{
    self.loginView = [[UIView alloc]initWithFrame:CGRectMake(10, 100, SCREEN_WIDTH- 20, SCREEN_HEIGHT)];
    [self.view addSubview:self.loginView];
    //
    //
    self.companyField.frame =CGRectMake(10,20, self.loginView.frame.size.width-20, 40);
   [self.loginView addSubview:self.companyField];
    //
    self.accountField.frame =CGRectMake(10,self.companyField.frame.origin.y+self.companyField.frame.size.height+10,self.loginView.frame.size.width-20, 40);
   [self.loginView addSubview:self.accountField];
    //
    self.pwdField.frame =CGRectMake(10,self.accountField.frame.origin.y+self.accountField.frame.size.height+10,self.loginView.frame.size.width-20, 40);
    [self.loginView addSubview:self.pwdField];
    //
    self.repwdField.frame =CGRectMake(10,self.pwdField.frame.origin.y+self.pwdField.frame.size.height+10,self.loginView.frame.size.width-20, 40);
    [self.loginView addSubview:self.repwdField];
    //
    self.sendButton.frame=CGRectMake(30,self.repwdField.frame.origin.y+self.repwdField.frame.size.height+20, self.loginView.frame.size.width-60, 50);
    [self.loginView  addSubview:self.sendButton];
    
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, self.sendButton.frame.origin.y+self.sendButton.frame.size.height+20, self.loginView.frame.size.width-20, 60)];
    titleLabel.text = @"温馨提示：找回密码仅适用于企业用户，专家用户请联系客服！";
    titleLabel.textColor = UIColorOfHex(0x666666);
    titleLabel.numberOfLines=2;
    [self.loginView addSubview:titleLabel];
}
-(LTLimitTextField *)companyField{
    if (!_companyField) {
        _companyField = [[LTLimitTextField alloc]initWithFrame:CGRectZero];
        _companyField.backgroundColor = [UIColor clearColor];
        _companyField.font = [UIFont systemFontOfSize:14.f];
        _companyField.placeholder = @"请输入所在企业营业执照";
        _companyField.keyboardType = UIKeyboardTypeDefault;
        _companyField.returnKeyType=UIReturnKeyNext;
        _companyField.textLimitInputType =LTTextLimitInputTypeNone;
        _companyField.borderStyle = UITextBorderStyleRoundedRect;
        _companyField.leftViewMode = UITextFieldViewModeAlways;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 34, 22)];
        imageView.image = [UIImage imageNamed:@"username_login"];
        imageView.contentMode = UIViewContentModeCenter;
        _companyField.leftView = imageView;
    }
    return _companyField;
}
-(LTLimitTextField *)accountField{
    if (!_accountField) {
        _accountField = [[LTLimitTextField alloc]initWithFrame:CGRectZero];
        _accountField.backgroundColor = [UIColor clearColor];
        _accountField.font = [UIFont systemFontOfSize:14.f];
        _accountField.placeholder = @"请输入账号";
        _accountField.keyboardType = UIKeyboardTypeDefault;
        _accountField.returnKeyType=UIReturnKeyNext;
        _accountField.textLimitInputType =LTTextLimitInputTypeNone;
        _accountField.borderStyle = UITextBorderStyleRoundedRect;
        _accountField.leftViewMode = UITextFieldViewModeAlways;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 34, 22)];
        imageView.image = [UIImage imageNamed:@"username_login"];
        imageView.contentMode = UIViewContentModeCenter;
        _accountField.leftView = imageView;
    }
    return _accountField;
}

-(LTLimitTextField *)pwdField{
    if (!_pwdField) {
        _pwdField = [[LTLimitTextField alloc]initWithFrame:CGRectZero];
        _pwdField.backgroundColor = [UIColor clearColor];
        _pwdField.font = [UIFont systemFontOfSize:14.f];
        _pwdField.placeholder = @"请输入新密码";
        _pwdField.keyboardType = UIKeyboardTypeDefault;
        _pwdField.returnKeyType=UIReturnKeyNext;
        _pwdField.textLimitInputType =LTTextLimitInputTypeNone;
        _pwdField.borderStyle = UITextBorderStyleRoundedRect;
        _pwdField.leftViewMode = UITextFieldViewModeAlways;
        _pwdField.secureTextEntry = YES;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 34, 22)];
        imageView.image = [UIImage imageNamed:@"password_login"];
        imageView.contentMode = UIViewContentModeCenter;
        _pwdField.leftView = imageView;
    }
    return _pwdField;
}

-(LTLimitTextField *)repwdField{
    if (!_repwdField) {
        _repwdField = [[LTLimitTextField alloc]initWithFrame:CGRectZero];
        _repwdField.backgroundColor = [UIColor clearColor];
        _repwdField.font = [UIFont systemFontOfSize:14.f];
        _repwdField.placeholder = @"请再次输入新密码";
        _repwdField.keyboardType = UIKeyboardTypeDefault;
        _repwdField.returnKeyType=UIReturnKeyDone;
        _repwdField.textLimitInputType =LTTextLimitInputTypeNone;
        _repwdField.borderStyle = UITextBorderStyleRoundedRect;
        _repwdField.leftViewMode = UITextFieldViewModeAlways;
        _repwdField.secureTextEntry = YES;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 34, 22)];
        imageView.image = [UIImage imageNamed:@"password_login"];
        imageView.contentMode = UIViewContentModeCenter;
        _repwdField.leftView = imageView;
    }
    return _repwdField;
}
- (UIButton *)sendButton{
    
    if (!_sendButton) {
        _sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _sendButton.layer.cornerRadius =20;
        _sendButton.layer.masksToBounds = YES;
        _sendButton.backgroundColor=UIColorOfHex(0x1E90FF);
        [_sendButton setTitle:@"确定" forState:UIControlStateNormal];
        [_sendButton addTarget:self action:@selector(loginClick) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _sendButton;
    
}
-(void)loginClick
{
    if (self.companyField.text.length==0) {
        [BaseToast show:@"请输入所在企业营业执照！"];
        return;
    }
    if (self.accountField.text.length==0) {
        [BaseToast show:@"请输入账号！"];
        return;
    }
    if (self.pwdField.text.length==0) {
        [BaseToast show:@"请输入新密码！"];
        return;
    }
    if (self.repwdField.text.length==0) {
        [BaseToast show:@"请再次输入新密码！"];
        return;
    }
    if (![self.pwdField.text isEqualToString:self.repwdField.text]) {
        [BaseToast show:@"两次密码不同，请重新设置密码！"];
        return;
    }
    __weak __typeof(self)weakSelf = self;
    [[Api sharedApi] forgetPwd:self.companyField.text account:self.accountField.text newPassword:self.pwdField.text callback:^(ApiResponse * _Nonnull response, NSString * _Nonnull errorMsg) {
        [BaseToast show:response.message];
        if(response.code==0 && response.data !=nil)
        {
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }
    }];
}
@end
